<?php

namespace App\Models;

use App\Util\Tool;
use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class CaseDescribe extends Model
{
    //对应前台>问诊->电话咨询->病情描述copy copy->新建
    protected $table="c_case_describes";

    public function getAllergyAttribute($value)
    {
        return json_decode($value);

    }

    public function getDiseasesHistoryAttribute($value)
    {
        return json_decode($value,true);

    }

    public function setPictureAttribute($gallery)
    {
        if (is_array($gallery)) {
            $this->attributes['picture'] = json_encode($gallery);
        }
    }

    public function getPictureAttribute($gallery)
    {
        return json_decode($gallery, true);
    }


    /**
     * @param $filter
     * @return array
     * 获取自己的病例没有关联
     */
    public function getMySelfCase($filter)
    {
        $where[] = ['user_id', '=', $filter['user_id']];
        $data = $this->where($where)->orderByDesc('id')->get();
        $tool = new Tool();
        foreach ($data as $k => $v) {
            $v->picture = $tool->addHostToImage($v->picture);
        }
        if (!empty($data)) {
            return $data->toArray();
        }
        return [];
    }



    /***
     * @param $filter
     * @return array
     * 病例列表
     */
    public function getMyCase($filter)
    {
        $whereor = [];
        $where[] = ['user_id', '=', $filter['user_id']];
        $res = $this->getRelationId($filter['user_id']);
        if (!empty($res)) {
            $whereor[] = [function ($query) use ($res) {
                return $query->whereIn('id', $res);
            }];
        }
        $data = $this->where($where)->orWhere($whereor)->orderByDesc('id')->get();
        $relat = new RelationshipCase();
        $host = new Url();
        $tool = new Tool();
        foreach ($data as $k => $v) {
            $v->is_my_relat = 0;
            $v->is_rela_state = 0;
            if ($v->user_id == $filter['user_id']) { //判断本人病例是否已经被关联过
                $had = $relat->where([['case_id', '=', $v->id]])->first();
                if (!empty($had)) {
                    $v->is_rela_state = 1;
                }
            } else {
                //判断该病例是否是关联病例
                $had = $relat->where([['case_id', '=', $v->id]])->first();
                if (!empty($had)) {
                    $v->is_my_relat = 1;
                }
            }
            $v->picture = $tool->addHostToImage($v->picture);
        }
        if (!empty($data)) {
            return $data->toArray();
        }
        return [];
    }


    public function getRelationId($user_id)
    {
       return  (new RelationshipCase())->where([['user_id','=',$user_id]])->pluck('case_id');
        
    }

    /***
     * @param $filter
     * @return array
     * 病例详情
     */
    public function getMyCaseDetail($filter)
    {
        $data = $this->where([['id', '=', $filter['id']]])->first();
        $host = new Url();
        if (!empty($data)) {
            if(is_array($data->picture)){
                $img = [];
                foreach($data->picture as $k=>$v){
                    $img[] = $host::getHttpHost().'upload/'.$v;
                }
            }
            $data->picture =$img;
            return $data->toArray();
        }
        return [];
    }
    
    
    /**
     * 更新病例
     */
    public function updateCase($filter)
    {
        $tool = new Tool();
        $data = $this->where([['id', '=', $filter['case_id']]])->first();
        $data->describe = isset($filter['describe']) ? $filter['describe'] : "";
        $data->picture = [];
        if (isset($filter['picture']) && !empty($filter['picture'])) {
            $data->picture = $tool::handelString($filter['picture']);
        }
        return $data->save();
    }

    /**
     * @param $data
     * @return int
     * 删除病例
     */
    public function delCaseDescribe($data)
    {
        return $this::destroy($data['id']);
    }

    
    
}
